﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using LevenBlog.Core.Install;
using LevenBlog.Core.Install.Operations;
using LevenBlog.Core.Utils;
using LevenBlog.Core.Entities;

namespace LevenBlog.Web.install {
    public partial class step3 : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            int step = Request.GetSetup();
            if (step < 2)
                Response.Redirect("step1.aspx");
            InstallConfigService.Write(new InstallConfig());
            (Master as install).SubTitle = "第三步 - 安装选项";
            setup_mode.Attributes.Add("onchange", "changeinstallmode();");
            rotute_selectext.Attributes.Add("onchange", "changerouteextend();");
            List<string> extends = new List<string>();
            foreach (ListItem item in rotute_selectext.Items) {
                extends.Add(item.Value);
            }
            InstallCheckHandle.Init(extends);
        }

        protected void step3_pre_Click(object sender, EventArgs e) {
            Response.Redirect("step2.aspx");
        }

        protected void step3_next_Click(object sender, EventArgs e) {
            int mode;
            if (!Int32.TryParse(setup_mode.SelectedValue, out mode)) {
                mode = 1;
            }
            //WebConfigItem config = new WebConfigItem() {
            //    AdminPasswordKey = setup_adminpasswordkey.Text,
            //    PasswordKey = setup_passwordkey.Text,
            //    PrefixKey = setup_prefixkey.Text,
            //    ProviderName = "LevenBlog.Core.Data.Sqlite.SqliteFactory, LevenBlog.Core"
            //};
            InstallConfig config = InstallConfigService.Read();
            config.AdminPasswordKey = setup_adminpasswordkey.Text;
            config.PasswordKey = setup_passwordkey.Text;
            config.PrefixKey = setup_prefixkey.Text;
            config.ProviderName = "LevenBlog.Core.Data.Sqlite.SqliteFactory, LevenBlog.Core";
            config.EnableShortRouting = route_enableshort.Checked;
            config.ExtendName = rotute_selectext.SelectedValue;
            string url = String.Empty;
            if (mode == 1) {
                //全新安装
                //config.ConnectionString = setup_new_datapath.Text;
                //IInstallOperation configOperation = new WebConfigInstall();
                //configOperation.Data = config;
                //Server.GetOperations().Add(configOperation);
                config.ConnectionString = setup_new_datapath.Text;
                lb_user user = new lb_user() {
                    name = setup_new_username.Text,
                    password = setup_new_password.Text.ToMD5(),
                    postnum = 0,
                    typeid = 3,
                    userinfo = new lb_userinfo() {
                        answer = String.Empty,
                        birthday = DateTime.Now,
                        face = "images/face.jpg",
                        lastloginip = Fetch.UserIp,
                        lastlogintime = DateTime.Now,
                        mail = setup_new_mail.Text,
                        nickname = setup_new_username.Text,
                        question = String.Empty,
                        regip = Fetch.UserIp,
                        regtime = DateTime.Now,
                        sex = 1,
                        signature = String.Empty,
                        userid = 1
                    }
                };
                config.DefaultUser = user;
                string indexUrl, tagUrl;
                if (route_enableshort.Checked) {
                    indexUrl = String.Format("~/index{0}", rotute_selectext.SelectedValue);
                    tagUrl = String.Format("~/tags{0}", rotute_selectext.SelectedValue);
                } else {
                    indexUrl = String.Format("~/blog/index{0}", rotute_selectext.SelectedValue);
                    tagUrl = String.Format("~/blog/tags{0}", rotute_selectext.SelectedValue);
                }
                config.DefaultCategories.Add(new lb_category() {
                    name = "Index",
                    info = "站点首页",
                    image = "images/icons/22.gif",
                    isurl = true,
                    url = indexUrl,
                    num = 0,
                    order = 1,
                    view = 0
                });
                config.DefaultCategories.Add(new lb_category() {
                    name = "Tags",
                    info = "标签云集",
                    image = "images/icons/3.gif",
                    isurl = true,
                    num = 0,
                    order = 2,
                    url = tagUrl,
                    view = 0
                });
                url = "step4.aspx";
            } else {
                //升级安装
                //config.ConnectionString = setup_update_datapath.Text;
                //IInstallOperation configOperation = new WebConfigInstall();
                //configOperation.Data = config;
                //IInstallOperation updateOperation = new SqliteConfigUpdateInstall();
                //Server.GetOperations().Add(configOperation);
                //Server.GetOperations().Add(updateOperation);
                config.ConnectionString = setup_update_datapath.Text;
                config.IsResetSkin = setup_update_skin.Checked;
                url = "step4.aspx?type=update";
            }
            InstallConfigService.Write(config);
            Response.SetSetup(3);
            Response.Redirect(url);
        }

        private void update() {
            
        }
    }
}
